package com.snake.rabbitmq.routing.producer;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.snake.rabbitmq.utils.MQConnectionUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 直连交换机
 * Direct exchange（直连交换机）是根据消息携带的路由键（routing key）将消息投递给对应队列的
 * @author : jjbin
 * @date : 14:22 2021/5/25
 */
public class RoutingProducer {

    //交换机名称
    private static final String DESTINATION_NAME = "my_routing_estination";

    public static void main(String[] args) throws IOException, TimeoutException {
        // 1. 建立mq连接
        Connection connection = MQConnectionUtils.newConnection();
        // 2. 创建通道
        Channel channel = connection.createChannel();
        // 3. 生产者绑定交换机 参数 1 交换机名称 参数2 交换机类型
        channel.exchangeDeclare(DESTINATION_NAME,"direct");
        String routingKey = "email";
        // 4.创建消息
        String msg = "my_fanout_estination hahhah" + routingKey;
        System.out.println("生产者投递消息:" + msg);
        // 5.发送消息 my_routing_estination  routing-key= email
        channel.basicPublish(DESTINATION_NAME,routingKey,null,msg.getBytes());
        // 6.关闭通道 跟连接
        channel.close();
        connection.close();
    }
}
